package com.xlz.day40encryptiondemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.xlz.day40encryptiondemo.utils.HttpUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {

    private String path="http://api.m.mtime.cn/Showtime/LocationMovies.api?locationId=%s";
    private String pathm="https://www.so.com/s?ie=utf-8&src=se7_errors&q=%E6%AF%9B%E6%B3%BD%E4%B8%9C";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //URLEncode编码

        String encode = null;
        try {
            encode = URLEncoder.encode("毛泽东","UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        pathm=pathm+encode+"s";

        //使用URLDecoder解码

      //  https://www.so.com/s?q=%E5%8C%97%E5%9B%BD%E9%A3%8E%E5%85%89%E5%8D%83%E9%87%8C%E5%86%B0%E5%B0%81%E4%B8%87%E9%87%8C%E9%9B%AA%E9%A3%98&src=srp_suggst_3.2.2&fr=se7_errors&psid=4fd48c7f834ee2cc92dda8af8b2d5294
        String str="%E5%8C%97%E5%9B%BD%E9%A3%8E%E5%85%89%E5%8D%83%E9%87%8C%E5%86%B0%E5%B0%81%E4%B8%87%E9%87%8C%E9%9B%AA%E9%A3%98";
        String decoder= null;
        try {
            decoder = URLDecoder.decode(str,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Log.d("flag", "------------------>onCreate: decode之后的内容" +decoder);

        new Thread(new Runnable() {
            @Override
            public void run() {
                InputStream is=null;
                ByteArrayOutputStream baos=null;

                try {
                    URL url=new URL(pathm);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(5000);
                    if(conn.getResponseCode()==200){
                        is=conn.getInputStream();
                        baos=new ByteArrayOutputStream();

                        int len=0;
                        byte[] buf=new byte[1024*8];
                        while ((len=is.read(buf))!=-1){
                            baos.write(buf,0,len);
                        }
                        byte[] bytes = baos.toByteArray();
                        Log.d("flag", "------------------>run: " +new String(bytes));
                    }

                } catch (MalformedURLException e) {
                    Log.d("flag", "------------------>run: ");
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (baos != null) {
                        try {
                            baos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
   //     initData();
    }
    private void initData() {
        HttpUtils.getByte(path);
    }
}
